
#用于与蓝牙建立连接
#这一部分有待增强健壮性
import setting
import time
from bluetooth import *
MAXLENGTH = 1024
class BlueConnector:
    __sock = BluetoothSocket(RFCOMM)
    def send(self,message):
        self.__sock.send(message)

    def read(self):
        return self.__sock.recv(MAXLENGTH)
    
    def reconnect(self):
        self.__sock.connect((setting.BLUE_HOST,setting.SERIAL_PORT))
    
    def disconnect(self):
        self.__sock.disconnect()

    def __init__(self):
        while True:
            try:
                self.reconnect()
                break
            except Exception as e:
                print("蓝牙连接失败，正在重连")
                os.system("sudo hcitool dc "+setting.BLUE_HOST)
                time.sleep(1)
